﻿Imports System.ComponentModel


<ToolboxBitmap(GetType(System.Windows.Forms.ComboBox)), DefaultEvent("SelectedIndexChanged")> _
Public Class PeriodBoxCtrl

#Region "Property Settings"

    Private _ConnectionString As String
    <Browsable(True), DefaultValue(""), Description("Connection string")> _
    Public Property zConnectionString() As String
        Get
            Return _ConnectionString
        End Get
        Set(ByVal value As String)
            _ConnectionString = value
            Connect()
        End Set
    End Property

    <Browsable(True), DefaultValue(""), Description("Period Box text")> _
    Public Property zText() As String
        Get
            Return TheBox.Text
        End Get
        Set(ByVal value As String)
            TheBox.Text = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("Period Box current selected Period_ID")> _
    Public Property zSelectedPeriod_ID() As Integer

        Get
            Return TheBox.SelectedValue
        End Get
        Set(ByVal value As Integer)
            TheBox.SelectedValue = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("Period Box current selected Index")> _
    Public Property SelectedIndex() As Integer

        Get
            Return TheBox.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            TheBox.SelectedIndex = value
        End Set
    End Property

    <Browsable(False), DefaultValue(""), Description("Period Box current selected Period_Name")> _
    Public ReadOnly Property zSelectedPeriod_Name() As String
        Get
            Try
                Return DirectCast(TheBox.SelectedItem, DataRowView).Item("Period_Name")
            Catch ex As Exception
                Return ""
            End Try

        End Get
    End Property

#End Region

    Public Custom Event SelectedIndexChanged As EventHandler
        AddHandler(ByVal value As EventHandler)
            AddHandler _TheBox.SelectedIndexChanged, value

        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler _TheBox.SelectedIndexChanged, value

        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

        End RaiseEvent
    End Event


    Private Function Connect() As Boolean

        If _ConnectionString = "" Then
            Return False
        End If

        Dim sql As String = "select Period_ID, Period_Name from dbo.Periods"
        Dim success As Boolean = True
        Dim dv As DataView

        dv = LoadDataView(sql, _ConnectionString, success)

        If success Then
            TheBox.Items.Clear()
            TheBox.DisplayMember = "Period_Name"
            TheBox.ValueMember = "Period_ID"
            TheBox.DataSource = dv
            Return True
        Else
            Return False
        End If

    End Function



End Class
